{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 逻辑回归-线性可分\n",
    "### 案例：根据学生的两门学生成绩，预测该学生是否会被大学录取\n",
    "#### 数据集：ex2data1.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Exam 1</th>\n",
       "      <th>Exam 2</th>\n",
       "      <th>Accepted</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>34.623660</td>\n",
       "      <td>78.024693</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>30.286711</td>\n",
       "      <td>43.894998</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>35.847409</td>\n",
       "      <td>72.902198</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>60.182599</td>\n",
       "      <td>86.308552</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>79.032736</td>\n",
       "      <td>75.344376</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Exam 1     Exam 2  Accepted\n",
       "0  34.623660  78.024693         0\n",
       "1  30.286711  43.894998         0\n",
       "2  35.847409  72.902198         0\n",
       "3  60.182599  86.308552         1\n",
       "4  79.032736  75.344376         1"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "path = 'ex2data1.txt'\n",
    "data = pd.read_csv(path, names=['Exam 1', 'Exam 2', 'Accepted'])\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYgAAAEKCAYAAAAIO8L1AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAIABJREFUeJzt3X2UHXWd5/H3NwEmhkUSmsBGY7ojZHQYVoKJgsMR5WFYdV0BxVEPM8sZYePszi5NsnsGZlDS7k7O0dkxEI6uTkZUdhJg1B0Hll1nhTi48wROAwEhyER5iBEkoSWMyoAJ/d0/qoquvtTtrr73VtWvqj6vc+rcvtX34dd1b9e3fk/fn7k7IiIineZVXQAREQmTAoSIiGRSgBARkUwKECIikkkBQkREMilAiIhIJgUIERHJpAAhIiKZFCBERCTTIVUXoB9HH320j4yMVF0MEZFaufvuu5929yWzPa7WAWJkZITx8fGqiyEiUitm9niex6mJSUREMhUWIMzsC2a218weSO07ysxuM7Nd8e3ieL+Z2bVm9j0zu9/M3lhUuUREJJ8iaxBfAt7Rse8KYLu7rwS2x/cB3gmsjLe1wGcLLJeIiORQWB+Eu/8/Mxvp2H0u8Pb45+uBO4DL4/3/w6Pc43ea2SIzW+ruTxZVPhGRAwcOsGfPHp5//vmqi1KIBQsWsGzZMg499N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kSCoBqESBdm9usWrSOyw8z+yMyG45z8R5vZPDP7KzM7J37sn8cJCx9MJS0krhV8Mv7d7Wb2ZjO7w8weMbP3ALj7z9z9r4kChUgwFCBEMpjZLwEfIEpGuAp4EXgbUaK3zwH/Cdjp7t+In/Jhd18NrAEuNbOheP/hwB3x734C/D7wq8D5wH8p6+8R6YWamESynUW0WM/fR+mFeAVRpt0xM3s/8FtEqTsSl5rZ+fHPryFaxGUC+DlR1lCI0iG84O4HzOw7RHn/RYKlACGSzYDr3f13p+2M1hBYFt/9Z8BPzOztRAkg3+Luz5nZHcCC+DEHfCqfzSTR6oa4+2Qq06lIkNTEJJJtO3BBKvPnUWY2TNTEtA24Cvjj+LFHAs/EweH1RKnkRWpPVzAiGdx9p5l9lGi1vHlE2TXXA28i6pd40czeZ2a/CdwA/JaZ3U+07OOdc32/eA2QVwKHmdl5wDnuvnNAf45IT5TNVUREMqmJSUREMilAiIhIJgUIERHJpAAhIiKZFCBERCSTAoSIiGRSgBARkUwKECIikun/A3+fxrmHHs2BAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig,ax = plt.subplots()\n",
    "ax.scatter(data[data['Accepted']==0]['Exam 1'],data[data['Accepted']==0]['Exam 2'],c='r',marker='x',label='y=0')\n",
    "ax.scatter(data[data['Accepted']==1]['Exam 1'],data[data['Accepted']==1]['Exam 2'],c='b',marker='o',label='y=1')\n",
    "ax.legend()\n",
    "ax.set(xlabel='exam1',\n",
    "          ylabel='exam2')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_Xy(data):\n",
    "    \n",
    "    data.insert(0,'ones',1)\n",
    "    X_ = data.iloc[:,0:-1]\n",
    "    X = X_.values\n",
    "    \n",
    "    y_ = data.iloc[:,-1]\n",
    "    y = y_.values.reshape(len(y_),1)\n",
    "    \n",
    "    return X,y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "X,y = get_Xy(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(100, 3)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(100, 1)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sigmoid(z):\n",
    "    \n",
    "    return 1 / (1 + np.exp(-z))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def costFunction(X,y,theta):\n",
    "    \n",
    "    A = sigmoid(X@theta)\n",
    "    \n",
    "    first = y * np.log(A)\n",
    "    second = (1-y) * np.log(1-A)\n",
    "    \n",
    "    return -np.sum(first + second) / len(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 1)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "theta = np.zeros((3,1))\n",
    "theta.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.6931471805599453\n"
     ]
    }
   ],
   "source": [
    "cost_init = costFunction(X,y,theta)\n",
    "print(cost_init)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def gradientDescent(X,y,theta,iters,alpha):\n",
    "    m = len(X)\n",
    "    costs = []\n",
    "    \n",
    "    for i in range(iters):\n",
    "        A = sigmoid(X@theta)\n",
    "        theta  = theta -(alpha/m)  * X.T @ (A - y)\n",
    "        cost = costFunction(X,y,theta)\n",
    "        costs.append(cost)\n",
    "        if i % 1000 == 0:\n",
    "            print(cost)\n",
    "    return costs, theta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "alpha = 0.004\n",
    "iters=200000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.9886538578930084\n",
      "3.0781700198641557\n",
      "5.487009635362496\n",
      "1.8519433661332647\n",
      "2.955128942408359\n",
      "5.438688730769618\n",
      "1.8046128090728513\n",
      "2.8312715776074056\n",
      "5.3886531633606145\n",
      "1.756153811712156\n",
      "2.7066763807478127\n",
      "5.3365673644957425\n",
      "1.7062807047694801\n",
      "2.5818508519054273\n",
      "5.281774101800353\n",
      "1.6544129958512053\n",
      "2.457843094743575\n",
      "5.223255119969365\n",
      "1.5996612097145546\n",
      "2.3362477519414626\n",
      "5.159653459570274\n",
      "1.5408871901849082\n",
      "2.2190492038468888\n",
      "5.089387088751128\n",
      "1.4768567851081469\n",
      "2.1082685090536497\n",
      "5.010845104587983\n",
      "1.4064493632805855\n",
      "2.005433890383271\n",
      "4.9225734611717336\n",
      "1.3288041261254437\n",
      "1.9109646054893665\n",
      "4.82325748482661\n",
      "1.243220225097169\n",
      "1.8235866121521405\n",
      "4.711246821296763\n",
      "1.1486277932139568\n",
      "1.7397667362447993\n",
      "4.5832574004573505\n",
      "1.0423478306006684\n",
      "1.6525865746034032\n",
      "4.430932868329269\n",
      "0.9166267386002196\n",
      "1.5465331276550862\n",
      "4.224624720512682\n",
      "0.7330944584991168\n",
      "1.453494463115446\n",
      "0.4241488960292939\n",
      "0.37316144916509664\n",
      "1.0073064982093751\n",
      "0.45806914812510036\n",
      "0.2417612484494892\n",
      "1.9687677995661368\n",
      "0.4579899262473645\n",
      "0.935969323241703\n",
      "0.47272531806309337\n",
      "3.513338804180419\n",
      "0.24314936601940193\n",
      "0.8718088010537077\n",
      "2.629729750247881\n",
      "0.4470391988813497\n",
      "0.3819594661727745\n",
      "0.3452946847765951\n",
      "2.9859501916238496\n",
      "2.8609606297730075\n",
      "0.23879213630448062\n",
      "2.4728559619202937\n",
      "1.0117755519515328\n",
      "2.5709866053097374\n",
      "0.6328254451047356\n",
      "0.6461075502697459\n",
      "0.4269071502513326\n",
      "2.4844259450577737\n",
      "0.2780784383960533\n",
      "2.3467621288899374\n",
      "1.875303660544557\n",
      "1.7129891892758928\n",
      "1.3804115972718984\n",
      "0.4353063951831528\n",
      "0.7845525934913954\n",
      "0.551867133215757\n",
      "0.5627433282580774\n",
      "0.5818779594764824\n",
      "0.629733923549564\n",
      "0.7101925174534659\n",
      "0.7314802322278338\n",
      "0.7546016126291198\n",
      "0.7799314853572015\n",
      "0.8079667201564134\n",
      "0.8394233130995006\n",
      "0.8754041526643763\n",
      "0.9177804970952285\n",
      "0.9702532175397057\n",
      "1.0425275815951671\n",
      "1.169721711921994\n",
      "1.1819699606800702\n",
      "1.165904433228842\n",
      "1.14990741291742\n",
      "1.133982422763924\n",
      "1.1181334945290606\n",
      "1.1023651305056192\n",
      "1.0866822584486289\n",
      "1.0710901811731584\n",
      "1.0555945209922293\n",
      "1.0402011593731837\n",
      "1.0249161724135096\n",
      "1.0097457629627316\n",
      "0.9946961904305567\n",
      "0.9797736995078687\n",
      "0.9649844491700111\n",
      "0.9503344434196646\n",
      "0.9358294652474968\n",
      "0.9214750152402627\n",
      "0.9072762561477257\n",
      "0.8932379645371796\n",
      "0.8793644904309875\n",
      "0.8656597255502928\n",
      "0.852127080497674\n",
      "0.8387694709178284\n",
      "0.8255893123981478\n",
      "0.8125885236222422\n",
      "0.7997685370823501\n",
      "0.7871303164960191\n",
      "0.774674379963439\n",
      "0.7624008278414649\n",
      "0.7503093742971515\n",
      "0.7383993815290028\n",
      "0.726669895702917\n",
      "0.7151196837323169\n",
      "0.7037472701313382\n",
      "0.692550973278043\n",
      "0.6815289405364328\n",
      "0.6706791817946062\n",
      "0.6599996010801088\n",
      "0.6494880260075127\n",
      "0.639142234896902\n",
      "0.628959981475163\n",
      "0.618939017132079\n",
      "0.6090771107540199\n",
      "0.5993720661972685\n",
      "0.5898217374930518\n",
      "0.5804240418986851\n",
      "0.5711769709233127\n",
      "0.5620785994655721\n",
      "0.5531270932034912\n",
      "0.5443207143760695\n",
      "0.5356578260914067\n",
      "0.5271368952891706\n",
      "0.5187564944760694\n",
      "0.5105153023423452\n",
      "0.50241210335534\n",
      "0.49444578641456277\n",
      "0.48661534264029543\n",
      "0.47891986235572825\n",
      "0.4713585313118257\n",
      "0.46393062619385433\n",
      "0.4566355094399692\n",
      "0.4494726233951992\n",
      "0.44244148381973775\n",
      "0.435541672767463\n",
      "0.4287728308510178\n",
      "0.4221346489123073\n",
      "0.4156268591226825\n",
      "0.4092492255454097\n",
      "0.40300153420342744\n",
      "0.39688358270867846\n",
      "0.3908951695236468\n",
      "0.38503608294196917\n",
      "0.3793060898914598\n",
      "0.37370492467898486\n",
      "0.36823227781164525\n",
      "0.36288778504159924\n",
      "0.35767101679157337\n",
      "0.35258146812405367\n",
      "0.34761854941781445\n",
      "0.34278157791211766\n",
      "0.3380697702687014\n",
      "0.3334822362870137\n",
      "0.32901797388803566\n",
      "0.32467586545703986\n",
      "0.32045467560771973\n",
      "0.31635305039863665\n",
      "0.3123695180014894\n",
      "0.30850249078794845\n",
      "0.30475026877130473\n",
      "0.3011110443110684\n",
      "0.2975829079639714\n",
      "0.29416385534494705\n",
      "0.290851794846427\n",
      "0.2876445560547326\n",
      "0.2845398986975681\n",
      "0.28153552195758763\n",
      "0.2786290739915835\n",
      "0.27581816150424027\n",
      "0.2731003592374333\n",
      "0.270473219250677\n",
      "0.26793427988456675\n",
      "0.265481074316027\n",
      "0.26311113863180763\n",
      "0.26082201936308735\n"
     ]
    }
   ],
   "source": [
    "costs,theta_final =  gradientDescent(X,y,theta,iters,alpha)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-23.77333549],\n",
       "       [  0.18687561],\n",
       "       [  0.18041961]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "theta_final"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "def predict(X,theta):\n",
    "    \n",
    "    prob = sigmoid(X@theta)\n",
    "    \n",
    "    return [1 if x >= 0.5 else 0 for x in prob]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.86\n"
     ]
    }
   ],
   "source": [
    "y_ = np.array(predict(X,theta_final))\n",
    "y_pre = y_.reshape(len(y_),1)\n",
    "\n",
    "acc  = np.mean(y_pre == y)\n",
    "\n",
    "print(acc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
